<template>
  <div>
    <el-checkbox :indeterminate="mIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
    <el-checkbox-group 
      v-model="mValue" 
      @change="handleChange">
      <el-checkbox v-for="(item,index) in codeData"
        :label="item.value"
        :key="index">
        {{item.text}}
      </el-checkbox>
    </el-checkbox-group>
  </div>
</template>
<script>
import {codeProvider} from '@/assets/js/quick/provider.js'
export default {
  name: 'bms-checkbox',
  props:{
    value : {type: Array ,default:''},
    codeType : {type:String,default:''},
    condition : {type:Object,default:()=>{}},
    indeterminate : {type:Boolean,default:false},
  },
  data(){
    return {
      mValue: this.value,
      codeData : [],
      allData: [],
      checkAll: false,
      mIndeterminate: this.indeterminate
    }
  },
  watch : {
    condition: {
      handler(newValue,oldValue) {
        this.initData();
      },
      deep: true
    },
    codeType: {
      handler(newValue,oldValue) {
        this.initData();
      },
      deep: true
    },
  },
  methods : {
    handleCheckAllChange(value) {
      this.mValue = value ? this.allData : [];
      this.mIndeterminate = false

      this.$emit("change",this.mValue)
      this.$emit("input",this.mValue)
    },
    handleChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.allData.length;
      this.mIndeterminate = checkedCount > 0 && checkedCount < this.allData.length;

      this.$emit("change",this.mValue)
      this.$emit("input",this.mValue)
    },
    async initData() {
      if(this.codeType) {
        let _this = this
        await codeProvider.getList(this.codeType,this.condition || {}).then(resp => {
          _this.codeData = resp
          _.forEach(resp, function(value, key) {
            _this.allData.push(value.value)
          });
          console.log(_this.codeData)
        }).catch(err => {
          _g.toastMsg("error",err)
        })
      } else {
        console.error('codeType is not empty!');
      }
    }
  },
  created(){
    this.initData();
  },
  mounted(){
  }
}
</script>
<style scoped lang="scss">

</style>

